package com.asion.qkan.ui.fragment;

import android.os.Bundle;
import android.support.v4.content.Loader;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.asion.qkan.R;
import com.asion.qkan.adapters.CommonRecyAdapter;
import com.asion.qkan.entry.MaterialBean;
import com.asion.qkan.ui.contract.MaterialContract;
import com.asion.qkan.ui.model.MaterialModel;
import com.asion.qkan.ui.presenter.MaterialPresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by HCJ on 2016/10/17.
 */

public class MaterialFragment extends BaseFragment<MaterialPresenter, MaterialModel> implements MaterialContract.View {
    @BindView(R.id.material_intro)
    TextView materialIntro;
    @BindView(R.id.material_recy)
    RecyclerView materialRecy;

    private String id;
    private CommonRecyAdapter<MaterialBean.DataBean.TopicsBean> adapter;

    @Override
    protected int getLayoutResource() {
        return R.layout.material_fragment;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
        mPresenter.setAllContentDatas(id);
    }

    @Override
    protected void initView() {
        Bundle arguments = getArguments();
        id = arguments.getString("id");
        //给RecyclerView设置适配器
        adapter = new CommonRecyAdapter<>(getActivity());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        materialRecy.setLayoutManager(linearLayoutManager);
        materialRecy.setAdapter(adapter);
    }

    @Override
    public void showDataSuccess(MaterialBean materialBean) {
        Log.e("tag",materialBean.toString());
        materialIntro.setText(materialBean.getData().getIntro());
        List<MaterialBean.DataBean.TopicsBean> topics = materialBean.getData().getTopics();
        adapter.addAll(topics);
    }

    @Override
    public void showDataError(Throwable throwable) {
        throwable.printStackTrace();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }
}
